//
//  DGBlockActionSheet.m
//  DGUIKit
//
//  Created by Jinxiao on 1/17/14.
//  Copyright (c) 2014 debugeek. All rights reserved.
//

#import "DGBlockActionSheet.h"

@interface DGBlockActionSheet () <UIActionSheetDelegate>
@property (readwrite, nonatomic, strong) NSMutableDictionary *buttonBlocks;
@end

@implementation DGBlockActionSheet

+ (id)actionSheetWithTitle:(NSString *)title
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
    return [[[self class] alloc] initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
#pragma clang diagnostic pop
}

- (NSMutableDictionary *)buttonBlocks
{
    if(!_buttonBlocks)
    {
        _buttonBlocks = [[NSMutableDictionary alloc] init];
    }
    return _buttonBlocks;
}

- (NSInteger)addButtonWithTitle:(NSString *)title clickBlock:(void (^)(void))clickBlock
{
    NSInteger buttonIndex = [super addButtonWithTitle:title];
    
    NSString *key = [NSString stringWithFormat:@"%li", (long)buttonIndex];
    
    if(clickBlock)
    {
        [self.buttonBlocks setObject:[clickBlock copy] forKey:key];
    }
    
    return buttonIndex;
}

- (NSInteger)addDestructiveButtonWithTitle:(NSString *)title clickBlock:(void (^)(void))clickBlock
{
    NSInteger buttonIndex = [self addButtonWithTitle:title clickBlock:clickBlock];
    
    [self setDestructiveButtonIndex:buttonIndex];
    
    return buttonIndex;
}

- (NSInteger)addCancelButtonWithTitle:(NSString *)title clickBlock:(void (^)(void))clickBlock
{
    NSInteger buttonIndex = [self addButtonWithTitle:title clickBlock:clickBlock];
    
    [self setCancelButtonIndex:buttonIndex];
    
    return buttonIndex;
}

- (NSInteger)addCancelButtonWithTitle:(NSString *)title
{
    NSInteger buttonIndex = [self addButtonWithTitle:title];
    
    [self setCancelButtonIndex:buttonIndex];
    
    return buttonIndex;
}

#pragma mark - UIActionSheetDelegate

+ (void)actionSheet:(DGBlockActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(actionSheet.didClickButtonBlock)
    {
        actionSheet.didClickButtonBlock(buttonIndex);
    }
    
    if(actionSheet.clickBlockType == DGBlockActionSheetClickBlockTypeDidClickButton)
    {
        void (^clickBlock) (void) = [actionSheet.buttonBlocks objectForKey:[NSString stringWithFormat:@"%li", (long)buttonIndex]];
        if(clickBlock)
        {
            clickBlock();
        }
    }
}

+ (void)actionSheetCancel:(DGBlockActionSheet *)actionSheet
{
    if(actionSheet.didCancelBlock)
    {
        actionSheet.didCancelBlock();
    }
}

+ (void)willPresentActionSheet:(DGBlockActionSheet *)actionSheet
{
    if(actionSheet.willPresentBlock)
    {
        actionSheet.willPresentBlock();
    }
}

+ (void)didPresentActionSheet:(DGBlockActionSheet *)actionSheet
{
    if(actionSheet.didPresentBlock)
    {
        actionSheet.didPresentBlock();
    }
}

+ (void)actionSheet:(DGBlockActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(actionSheet.willDismissBlock)
    {
        actionSheet.willDismissBlock(buttonIndex);
    }
}

+ (void)actionSheet:(DGBlockActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(actionSheet.didDismissBlock)
    {
        actionSheet.didDismissBlock(buttonIndex);
    }
    
    if(actionSheet.clickBlockType == DGBlockActionSheetClickBlockTypeDidDismiss)
    {
        void (^clickBlock) (void) = [actionSheet.buttonBlocks objectForKey:[NSString stringWithFormat:@"%li", (long)buttonIndex]];
        if(clickBlock)
        {
            clickBlock();
        }
    }
}


@end